package com.joven.shelltest.utils.encrypt;

/**
 * @author:
 * @since: 2023-08-07
 */

import java.security.MessageDigest;

public class Md5Utils {

    public static String hash(String input) {
        try {
            // 创建MD5摘要算法实例
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 将要计算的数据转换为字节数组
            byte[] inputBytes = input.getBytes();
            // 计算字节数组的MD5哈希值
            byte[] hashBytes = md.digest(inputBytes);

            // 将字节数组转换为十六进制字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : hashBytes) {
                sb.append(String.format("%02x", b & 0xff));
            }

            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}